/*
 * Copyright (C) 2012 Michael Widmer michael.widmer@post.ch
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.core.connectivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import java.util.List;

/**
 * This class is used to check if the user has installed and/or enabled all the
 * required components on his device.
 * @author Griz
 */
public class RequirementsChecker extends Activity {

    public static final int DATA_NETWORK_WIFI = 1;
    public static final int DATA_NETWORK_WIMAX = 2;
    public static final int DATA_MOBILE_STANDARD = 4;
    public static final int DATA_MOBILE_HI = 8;
    
    private static final int TTS_CHECK_REQUEST = 3369;
    
    private ConnectivityManager mDataManager;
    private boolean mTtsChecked;
    private boolean mGpsChecked;
    private boolean mTtsInstalled;
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        mTtsChecked = false;
        mGpsChecked = false;
        
        mDataManager = (ConnectivityManager) super.getSystemService(Context.CONNECTIVITY_SERVICE);
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onResume() {
        super.onResume();
        Intent data = new Intent();
        /*if(mTtsChecked){
            if(!mTtsInstalled) {
                startActivity(new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA));
                setResult(Activity.RESULT_CANCELED);
                finish();
            }
        } else {
            startActivityForResult(new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA), TTS_CHECK_REQUEST);
        }
        if(!mGpsChecked) {
            mGpsChecked = true;
            if(hasGps()) {
                data.putExtra("gpsAvailable", true);
                boolean avail = checkGps();
                data.putExtra("gpsEnabled", avail);
                if(!avail) {
                    Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(gpsOptionsIntent);
                }
            } else {
                data.putExtra("gpsAvailable", false);
            }
        }//*/        
        setResult(Activity.RESULT_OK, data);
        finish();
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TTS_CHECK_REQUEST) {
            mTtsChecked = true;
            mTtsInstalled = (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS);                
        }
    }
    
    /*
    private boolean hasGps() {
        LocationManager lm = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = lm.getAllProviders();
        return (providers != null && providers.contains(LocationManager.GPS_PROVIDER));
    }
    
    private boolean checkGps() {
        LocationManager lm = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }*/
    
    /**
     * Returns a bitmask representing the available DataConnections.
     * @return 
     * @see #DATA_MOBILE_HI
     * @see #DATA_MOBILE_STANDARD
     * @see #DATA_NETWORK_WIFI
     * @see #DATA_NETWORK_WIMAX
     */
    public int getConnectionMask() {
        int mask = 0;
    
        NetworkInfo wifi = mDataManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo wimax = mDataManager.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
        NetworkInfo mobstd = mDataManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo mobhi = mDataManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI);
        
        if(wifi != null && wifi.isAvailable() && wifi.isConnected())  {
            mask |= DATA_NETWORK_WIFI;
        }
        if(wimax != null && wimax.isAvailable()&& wimax.isConnected()) {
            mask |= DATA_NETWORK_WIMAX;
        }
        
        if(mobstd != null && mobstd.isAvailable() && mobstd.isConnected()) {
            mask |= DATA_MOBILE_STANDARD;
        }
        if(mobhi != null && mobhi.isAvailable() && mobhi.isConnected()) {
            mask |= DATA_MOBILE_HI;
        }
        return mask;
    }
    
    //TODO: check connection speed 
    
    /**
     * Indicates if a data connection is available.
     * @return 
     */
    public boolean hasDataConnection() {
        return getConnectionMask() != 0;
    }

    /**
     * Indicates if a gps connection is available.
     * @return 
     */
    public boolean hasGpsConnection() {
        return true;
    }
    
    /**
     * Indicates if text-to-speech is installed.
     * @return 
     */
    public boolean hasTtsInstalled() {
        return true;
    }
}
